package com.sso.action.sys;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sso.entity.view.SysMenuResourceView;
import com.sso.exception.DataBaseException;
import com.sso.manager.SysMenuResourceViewManager;
import com.sso.manager.SysResourceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.tools.util.JSONUtils;
import com.tools.util.RequestUtils;
import com.tools.util.StrUtils;
import com.sso.action.BaseAction;
import com.sso.common.enums.FlagStatus;
import com.sso.common.enums.SysResourceEnum;
import com.sso.entity.Page;
import com.sso.entity.SysMenu;
import com.sso.entity.SysResource;
import com.sso.entity.vo.SysResourceVo;
import com.sso.exception.SelectException;
import com.sso.manager.SysMenuManager;

/**
 * 权限资源action
 * @author Admin
 *
 */
@Controller()
@RequestMapping("/res")
class SysResourceAction extends BaseAction{

	@Autowired
	private SysResourceManager resManager;
	@Autowired 
	private SysMenuManager menuManager;
	@Autowired
	private SysMenuResourceViewManager menuResourceViewManager;
	
	/**
	 * 资源管理页面
	 * @return
	 */
	@RequestMapping("gotoResourceView")
	public ModelAndView sysResourceView(HttpServletRequest request,HttpServletResponse response){
		ModelAndView view = new ModelAndView("/manager/resourceManager");
		try{
			Map<String,Object> filterMap = RequestUtils.getParametersToMap(request, "");
			Page<SysResourceVo> resourcePage = resManager.selectByPage(filterMap);
			view.addObject("page", resourcePage);
		}catch(Exception e){
			e.printStackTrace();
		}
		return view;
	}

    /**
     * 根据action和method查询
     * @param request
     * @param response
     */
	@RequestMapping("getResourceByLikeKey")
	public void getResourceByKey(HttpServletRequest request,HttpServletResponse response){
		try {
			Map<String,Object> filterMap = RequestUtils.getParametersToMap(request, "");
			List<SysResourceVo> list = resManager.selectVoByProperty(filterMap);
			if(list.size()==0){
				this.outputJson(FlagStatus.ERROR.value,"未查询到任何数据",response);
				return;
			}
			//需要转换的属性
			String attr = "id,action,method,url,chineseStatus";
			Object json = JSONObject.parse(JSONUtils.toJsonInAttrbuite(list, attr));
			this.outputJson(FlagStatus.SUCCESS.value,json, response);
		} catch (Exception e) {
			e.printStackTrace();
			this.outputJson(FlagStatus.ERROR.value,"查询失败，请联系管理人员",response);
		}
	}
	
	@RequestMapping("stopAccess")
	public void stopAccess(String resourceId,HttpServletRequest request,HttpServletResponse response){
		try {
			String status = FlagStatus.ERROR.value;
			if(resourceId==null || StrUtils.isEmpty(resourceId)){
				this.outputJson(status, "未查询到要停止访问的资源", response);
				return;
			}
			SysResource res = resManager.selectByid(resourceId.trim());
			if(res==null || StrUtils.isEmpty(res.getId())){
				this.outputJson(status, "未查询到要停止访问的资源", response);
				return;
			}
			if(SysResourceEnum.status.DEL.key.equals(res.getStatus())){
				this.outputJson(status, "该资源已被删除无法停止", response);
				return;
			}
			if(SysResourceEnum.status.STOP.key.equals(res.getStatus())){
				this.outputJson(status, "该资源已被停止请勿重复操作", response);
				return;
			}
			res.setStatus(SysResourceEnum.status.STOP.key);
			resManager.saveResource(res);
			this.outputJson(FlagStatus.SUCCESS.value, "停用成功", response);
		} catch (Exception e) {
			e.printStackTrace();
			this.outputJson(FlagStatus.ERROR.value, "操作失败，请联系管理人员", response);
		}
	}
	
	@RequestMapping("startAccess")
	public void startAccess(String resourceId,HttpServletRequest request,HttpServletResponse response){
		try {
			String status = FlagStatus.ERROR.value;
			if(resourceId==null || StrUtils.isEmpty(resourceId)){
				this.outputJson(status, "未查询到要开启访问的资源", response);
				return;
			}
			SysResource res = resManager.selectByid(resourceId.trim());
			if(res==null || StrUtils.isEmpty(res.getId())){
				this.outputJson(status, "未查询到要开启访问的资源", response);
				return;
			}
			if(SysResourceEnum.status.START.key.equals(res.getStatus())){
				this.outputJson(status, "该资源已被开启请勿重复操作", response);
				return;
			}
			res.setStatus(SysResourceEnum.status.START.key);
			resManager.saveResource(res);
			this.outputJson(FlagStatus.SUCCESS.value, "启用成功", response);
		} catch (Exception e) {
			e.printStackTrace();
			this.outputJson(FlagStatus.SUCCESS.value, "操作失败，请联系管理人员", response);
		}
	}

	@RequestMapping("deleteAccess")
	public void deleteAccess(String resourceId,HttpServletRequest request,HttpServletResponse response){
		try {
			String status = FlagStatus.ERROR.value;
			if(resourceId==null || StrUtils.isEmpty(resourceId)){
				this.outputJson(status, "未查询到要删除访问的资源", response);
				return;
			}
			SysResource res = resManager.selectByid(resourceId.trim());
			if(res==null || StrUtils.isEmpty(res.getId())){
				this.outputJson(status, "未查询到要删除访问的资源", response);
				return;
			}
			if(SysResourceEnum.status.DEL.key.equals(res.getStatus())){
				this.outputJson(status, "该资源已被删除", response);
				return;
			}
			res.setStatus(SysResourceEnum.status.DEL.key);
			resManager.saveResource(res);
			this.outputJson(FlagStatus.SUCCESS.value, "删除成功", response);
		} catch (Exception e) {
			e.printStackTrace();
			this.outputJson(FlagStatus.SUCCESS.value, "操作失败，请联系管理人员", response);
		}
	}

	@RequestMapping("saveAuthType")
	public void saveAuthType(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> param = RequestUtils.getParametersToMap(request, "");
		String flag = FlagStatus.ERROR.value;
		String type;
		String resourceId;
		try{
			//参数校验
			if(!param.containsKey("authType")){
				this.outputJson(flag,"请选择权限类型", response);
				return;
			}
			if(!param.containsKey("resourceId")){
				this.outputJson(flag, "要设置的资源不存在", response);
				return;
			}
			type = (String) param.get("authType");
			resourceId = (String) param.get("resourceId");
			SysMenuResourceView menuResourceView = menuResourceViewManager.selectByResourceId(resourceId);
			if(StrUtils.isNotEmpty(menuResourceView.getMenuId())){
				this.outputJson(flag, "该资源已经绑定菜单不能修改权限！", response);
				return;
			}
			SysResource resource = resManager.selectByid(resourceId);
			Map<String,Object> filterMap = new HashMap<String, Object>();
			filterMap.put("resourceId", resource.getId());
			resManager.saveAuthType(type, resource);
			this.outputJson(FlagStatus.SUCCESS.value, "设置成功", response);
		}catch(Exception e){
			e.printStackTrace();
			this.outputJson(FlagStatus.ERROR.value,"操作失败，请联系管理人员",response);
		}
	}
	
	@RequestMapping("getResourceByMenuId")
	public void getResourceByMenuId(HttpServletRequest request,HttpServletResponse response) throws SelectException {
		Map<String, Object> param = RequestUtils.getParametersToMap(request, "");
		String menuId = (String) param.get("menuId");
		String status = FlagStatus.ERROR.value;
		if(StrUtils.isEmpty(menuId)){
			this.outputJson(status, "请选择要查询的菜单", response);
			return;
		}
		SysMenu menu = menuManager.selectById(menuId);
		if(menu==null || StrUtils.isEmpty(menu.getId())){
			this.outputJson(status, "未找到要查询的菜单信息", response);
			return;
		}
		if(StrUtils.isEmpty(menu.getResourceId())){
			this.outputJson(status, "所选菜单没有绑定资源", response);
			return;
		}
		SysResourceVo resource = resManager.selectVoById(menu.getResourceId());
		if(resource==null || StrUtils.isEmpty(resource.getId())){
			this.outputJson(status, "所选菜单绑定的资源不存在", response);
			return;
		}
		String fix = "id,action,method,url,chineseStatus";
		Object json = JSONObject.parse(JSONUtils.toJsonInAttrbuite(resource,fix));
		this.outputJson(FlagStatus.SUCCESS.value, json, response);
	}
}
